/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Xceed.Silverlight.ListBox.PositioningFunction
{
  internal class ZIndexFunction : IZIndexFunction
  {
    internal static int MaxZIndex = 99999;
    internal static int NoZIndex = -1;

    #region Constructor

    internal ZIndexFunction()
    {
      ZIndexStops = new ObservableCollection<ZIndexStop>();
    }

    #endregion

    #region ZIndexStops Property

    internal ObservableCollection<ZIndexStop> ZIndexStops
    {
      get;
      set;
    }

    #endregion

    #region Private Methods

    private double ApplyLinearInterpolation( double x, double x1, double x2, double y1, double y2 )
    {
      double m = ( ( y2 - y1 ) / ( x2 - x1 ) );
      double b = y1 - m * x1;
      return m * x + b;
    }

    #endregion

    #region IZIndexFunction Members

    int IZIndexFunction.GetZIndex( int percentage, bool forcedInViewport, bool isGroupDataItem )
    {
      double offset = ( double )percentage / 100.0;

      // Initialize the first stop
      ZIndexStop lastStop = this.ZIndexStops[ 0 ];

      // Sort the scaling stops list
      IOrderedEnumerable<ZIndexStop> stops = this.ZIndexStops.OrderBy( zIndexStop => zIndexStop.Offset );

      foreach( ZIndexStop zIndexStop in stops )
      {
        double lastOffset = lastStop.Offset;

        // if the current offset on the path is part of the current interval
        if( offset < zIndexStop.Offset )
        {
          // Compute the interpolated scaling factor according to the interval and the current position
          double factor = this.ApplyLinearInterpolation( lastOffset + ( offset - lastOffset ),
                                                         lastOffset,
                                                         zIndexStop.Offset,
                                                         lastStop.ZIndexFactor,
                                                         zIndexStop.ZIndexFactor );

          factor = Math.Min( 1, factor );
          factor = Math.Max( 0, factor );
          return ( int )( ZIndexFunction.MaxZIndex * factor );
        }

        // Keep a refenrce of the last stop
        lastStop = zIndexStop;
      }

      return 9999;
    }

    #endregion
  }
}
